﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/15 15:21:21
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 回复帖子实体类。
    /// </summary>
    public class Post : BaseObject
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Post"/>。
        /// </summary>
        public Post() {
            this.SettingsID = SiteSettings.Current.SettingsID;
            this.UserID = AppContext.Current.User.UserID;
            this.UserIP = AppContext.Current.Context.Request.UserHostAddress;
            this.CreateDate = DateTime.Now;
        }

        /// <summary>
        /// 从数据库中加载数据。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        protected override void Load(System.Data.IDataReader reader)
        {
            this.PostID = reader.GetInt32("PostID");
            this.SettingsID = reader.GetInt32("SettingsID");
            this.ThreadID = reader.GetInt32("ThreadID");
            this.UserID = reader.GetInt32("UserID");
            this.Body = reader.GetString("Body");
            this.CreateDate = reader.GetDateTime("CreateDate");
            this.LastModified = reader.GetDateTime("LastModified");
            this.UserIP = reader.GetString("UserIP");
            this.IsApproved = reader.GetBoolean("IsApproved");
        }

        /// <summary>
        /// 帖子ID。
        /// </summary>
        public int PostID { get; set; }

        /// <summary>
        /// 主题ID。
        /// </summary>
        public int ThreadID { get; set; }

        /// <summary>
        /// 网站设置ID。
        /// </summary>
        public int SettingsID { get; set; }

        /// <summary>
        /// 用户ID。
        /// </summary>
        public int UserID { get; set; }

        /// <summary>
        /// 内容。
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// 创建日期。
        /// </summary>
        public DateTime CreateDate { get; set; }

        /// <summary>
        /// 最后更新日期。
        /// </summary>
        public DateTime LastModified { get; set; }

        /// <summary>
        /// 用户IP。
        /// </summary>
        public string UserIP { get; set; }

        /// <summary>
        /// 是否通过验证。
        /// </summary>
        public bool IsApproved { get; set; }

        /// <summary>
        /// 获取格式化后的字符串。
        /// </summary>
        public string FormattedBody {
            get {
                if (Body == null)
                    return null;
                return Body.Replace("\r\n", "<br />");
            }
        }

        private User user;
        /// <summary>
        /// 获取帖子用户，如果不存在，则设置为当前用户。
        /// </summary>
        public User User {
            get {
                if (user == null)
                    user = Users.GetUser(this.UserID);
                if (user == null)
                    user = AppContext.Current.User;
                return user;
            }
        }
    }
}